package cn.redragon.soa.amqp.handler;


import cn.redragon.soa.amqp.model.InnerEventProperty;
import cn.redragon.soa.amqp.model.StockUpdatedEvent;
import cn.redragon.soa.common.amqp.model.CommonIdentity;
import cn.redragon.soa.common.amqp.model.ExchangeRoutingKey;
import cn.redragon.soa.common.amqp.model.Identity;
import cn.redragon.soa.common.amqp.model.RoutingKeyProperty;
import cn.redragon.soa.common.amqp.sender.AbstractEventSender;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class StockEventSender extends AbstractEventSender {

    private final RabbitTemplate template;
    private final InnerEventProperty property;

    public void triggerStockHistory(String appCode, StockUpdatedEvent event) {
        ExchangeRoutingKey cfg = getInnerExchangeRoutingKey(getEventProperty().getStock().getUpdated());
        this.trigger(getIdentity(appCode), event, cfg);
    }

    @Override
    public RabbitTemplate getTemplate() {
        return template;
    }

    public InnerEventProperty getEventProperty() {
        return property;
    }

    protected ExchangeRoutingKey getInnerExchangeRoutingKey(RoutingKeyProperty prop) {
        return super.getExchangeRoutingKey(property.getExchangeName(), prop);
    }

    private Identity getIdentity(String appCode) {
        return CommonIdentity.builder().brand(appCode).build();
    }
}
